﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Medical_Store_Billing.Data_Access_Layer;
using Medical_Store_Billing.Object_Class;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace Medical_Store_Billing.Business_Login_Layer
{
    class NhanvienBLL
    {
        NhanvienDAL m_nhanviendal = new NhanvienDAL();
        nhanvienclass m_nhanvienclass = null;
        public int Dangnhap(string user, string pass)
        {
            DataTable m_tb = m_nhanviendal.Laydsnhanvien(user);
            if (m_tb.Rows.Count == 0)
                return 0;
            string m_manv = m_tb.Rows[0]["manv"].ToString();
            string m_pass = m_tb.Rows[0]["pass"].ToString();

            if (m_pass == MaHoaMD5(pass))
            {
                if (m_manv == "NV001")
                    return 1;
                else
                {
                    return 2;
                }
            }
            else
            {
                return 3;
            }
        }

        public static string MaHoaMD5(string text)
        {
            MD5CryptoServiceProvider _md5Hasher = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(text);
            bs = _md5Hasher.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2"));
            }
            return s.ToString();
        }

        public DataTable Timnv(string name)
        {
            return m_nhanviendal.Timnv(name);
        }

       

        public DataTable Laydsnv()
        {
            return m_nhanviendal.Laydsnhanvien1();
        }

        public bool Themnv(string manv, string tennv, string sdt, string user, string pass)
        {
            try
            {
                m_nhanvienclass = new nhanvienclass();

                m_nhanvienclass.manv = manv;
                m_nhanvienclass.tennv = tennv;
                m_nhanvienclass.sodt = sdt;
                m_nhanvienclass.user = user;
                m_nhanvienclass.pass = MaHoaMD5(pass);

                return m_nhanviendal.Themnv(m_nhanvienclass);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }

        public bool Suanv(string manv, string tennv, string sdt, string user, string pass)
        {
            try
            {
                m_nhanvienclass = new nhanvienclass();

                m_nhanvienclass.manv = manv;
                m_nhanvienclass.tennv = tennv;
                m_nhanvienclass.sodt = sdt;
                m_nhanvienclass.user = user;
                m_nhanvienclass.pass = MaHoaMD5(pass);

                return m_nhanviendal.Suanv(m_nhanvienclass);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }
    }
}
